package role

import (
	"context"
	"fmt"
	"net/http"
	"wms/internal/svc"
	"wms/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AllLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAllLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AllLogic {
	return &AllLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AllLogic) All() (resp *types.RoleListResponse, err error) {
	resp = new(types.RoleListResponse)

	//1.查询分页
	roles, err := l.svcCtx.RoleModel.FindByPage(l.ctx, "")
	if err != nil {
		fmt.Printf("[Error]全部角色查询：%s\n", err.Error())
		resp.Code = http.StatusInternalServerError
		resp.Msg = "服务内部错误"
		return resp, nil
	}

	resp.Code = http.StatusOK
	resp.Msg = "成功"
	for _, one := range roles {
		resp.Data.List = append(resp.Data.List, types.Role{
			Id:        one.Id,
			SortId:    one.SortId,
			Status:    one.Status,
			Name:      one.Name,
			Remark:    one.Remark,
			CreatedAt: one.CreatedAt.Unix(),
			UpdatedAt: one.UpdatedAt.Unix(),
		})
	}

	return resp, nil
}
